package hk.cic.demointegrationapp;

import hk.cic.cwrs.bio.integration.log.IBioService;

//import android.media.AudioManager;
//import android.media.SoundPool;
import android.app.PendingIntent;
import android.content.SharedPreferences;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.NfcA;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.os.IBinder;

import java.util.HashMap;
import java.lang.Integer;

import android.media.AudioManager;
import android.media.SoundPool;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.cily.utils.log.L;
import com.mfrs.demos.SettingsActivity;
import com.mfrs.demos.TestActivity;

public class MainActivity extends AppCompatActivity {
   TextView tv_gps;
    private LocationClient locationClient = null;
    private static final int UPDATE_TIME = 60000;
    private  int LOCATION_COUTNS = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_gps = (TextView)findViewById(R.id.tv_gps);
        Intent i = new Intent(this, IcService.class);
        startService(i);
        initGPS();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (locationClient != null && locationClient.isStarted()) {
            locationClient.stop();
            locationClient = null;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (locationClient != null && locationClient.isStarted()) {
            locationClient.stop();
            locationClient = null;
        }
    }

    private void initGPS(){
        locationClient = new LocationClient(this);
        //设置定位条件
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);        //是否打开GPS
        option.setCoorType("bd09ll");       //设置返回值的坐标类型。
        option.setPriority(LocationClientOption.NetWorkFirst);  //设置定位优先级
        option.setProdName("LocationDemo"); //设置产品线名称。强烈建议您使用自定义的产品线名称，方便我们以后为您提供更高效准确的定位服务。
        option.setScanSpan(UPDATE_TIME);    //设置定时定位的时间间隔。单位毫秒
        locationClient.setLocOption(option);
        //注册位置监听器
        locationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                // TODO Auto-generated method stub
                if (location == null ) {
                    return;
                }
                LOCATION_COUTNS ++;
                String gps = location.getLatitude() + "," + location.getLongitude();
                tv_gps.setText("GPS:"+gps);
                SharedPreferences sp = getSharedPreferences("sp_setting", 0);
                SharedPreferences.Editor ed = sp.edit();
                ed.putString("gps", gps);
                ed.commit();
            }

            @Override
            public void onReceivePoi(BDLocation bdLocation) {

            }
        });
        locationClient.start();
        locationClient.requestLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
            case R.id.action_settings:
                Intent i = new Intent(MainActivity.this, SettingActivity.class);
                this.startActivity(i);
                break;

            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }
    private void save(String value){
        SharedPreferences sp = getSharedPreferences("sp_setting", 0);
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("server_host", value);
        ed.commit();
    }

    public String getSp(){
        SharedPreferences sp = getSharedPreferences("sp_setting", 0);
        return sp.getString("server_host", "");
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onNewIntent(Intent intent) {
        Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
        setIntent(intent);
    }


    @Override
    public void onPause() {
        super.onPause();
    }



}
